#include "screenmanager.h"
#include "qterminal.h"
#include "_def_p.h"
#include <QStringList>

bool ScreenManager::startSysDesktop()
{
    bool ret = true;
    QTerminal terminal(TOKEN);
    ret = ret && terminal.runAsAdmin("rm -f /etc/profile.d/EttaM1-autostart.sh >/dev/null");
    return ret;
}

bool ScreenManager::blockSysDesktop()
{
    bool ret = true;
    QTerminal terminal(TOKEN);
    QStringList cmdlist;
    cmdlist << "#!/bin/bash"
            << "exec_name=EttaM1"
            << "autostart_dir=/etc/profile.d"
            << "cur_user=$(who | awk '{print $1}')"
            << "exec_home=/home/${cur_user}/${exec_name}"
            << "shell_file=/tmp/${exec_name}-autostart.sh"
            << "make_autostart_shell() {"
            << "  echo \"#!/bin/bash\""
            << "  echo \"cd ${exec_home}\""
            << "  echo \"./${exec_name}\""
            << "}"
            << "make_autostart_shell >${shell_file}"
            << "mv -f ${shell_file} ${autostart_dir}";

    ret = ret && terminal.runAsAdmin(cmdlist);
    return ret;
}

bool ScreenManager::blockHibernate()
{
    bool ret = true;
    QTerminal terminal(TOKEN);
    QStringList cmdlist;
    cmdlist << "#!/bin/bash"
            << "x11_conf_dir=/etc/X11"
            << "config_file=/tmp/xorg.conf"
            << "make_screen_off_config() {"
            << "  echo 'Section \"ServerFlags\"'"
            << "  echo '  Option \"BlankTime\" \"0\"'"
            << "  echo '  Option \"StandbyTime\" \"0\"'"
            << "  echo '  Option \"SuspendTime\" \"0\"'"
            << "  echo '  Option \"OffTime\" \"0\"'"
            << "  echo 'EndSection'"
            << "}"
            << "make_screen_off_config >${config_file}"
            << "mv -f ${config_file} ${x11_conf_dir}";

    ret = ret && terminal.runAsAdmin(cmdlist);
    return ret;
}
